
Distances can be computed between trajectories as well as between trajectories and other geometry objects. The implemented distance measures are:
import pandas as pd
import geopandas as gpd
import movingpandas as mpd
import shapely as shp
import hvplot.pandas
import matplotlib.pyplot as plt
from geopandas import GeoDataFrame, read_file
from shapely.geometry import Point, LineString, Polygon
from datetime import datetime, timedelta
from holoviews import opts, dim
import warnings
warnings.filterwarnings('ignore')
plot_defaults = {'linewidth':5, 'capstyle':'round', 'figsize':(9,3), 'legend':True}
opts.defaults(opts.Overlay(active_tools=['wheel_zoom']))
hvplot_defaults = {'tiles':'CartoLight', 'frame_height':320, 'frame_width':320, 'cmap':'Viridis', 'colorbar':True}
mpd.show_versions()
MovingPandas 0.11.rc1 SYSTEM INFO ----------- python : 3.9.13 | packaged by conda-forge | (main, May 27 2022, 16:50:36) [MSC v.1929 64 bit (AMD64)] executable : H:\miniconda3\envs\movingpandas\python.exe machine : Windows-10-10.0.19043-SP0 GEOS, GDAL, PROJ INFO --------------------- GEOS : None GEOS lib : None GDAL : 3.5.0 GDAL data dir: None PROJ : 9.0.0 PROJ data dir: H:\miniconda3\pkgs\proj-9.0.0-h1cfcee9_1\Library\share\proj PYTHON DEPENDENCIES ------------------- geopandas : 0.11.1 pandas : 1.4.3 fiona : 1.8.21 numpy : 1.23.1 shapely : 1.8.2 rtree : 1.0.0 pyproj : 3.3.0 matplotlib : 3.5.2 mapclassify: 2.4.3 geopy : 2.2.0 holoviews : 1.14.8 hvplot : 0.8.0 geoviews : 1.9.5 stonesoup : 0.1b9
df = pd.DataFrame([
{'geometry':Point(0,0), 't':datetime(2018,1,1,12,0,0)},
{'geometry':Point(6,0), 't':datetime(2018,1,1,12,6,0)},
{'geometry':Point(6,6), 't':datetime(2018,1,1,12,10,0)},
{'geometry':Point(9,9), 't':datetime(2018,1,1,12,15,0)}
]).set_index('t')
geo_df = GeoDataFrame(df, crs=31256)
toy_traj = mpd.Trajectory(geo_df, 1)
toy_traj.df
| geometry | |
|---|---|
| t | |
| 2018-01-01 12:00:00 | POINT (0.000 0.000) |
| 2018-01-01 12:06:00 | POINT (6.000 0.000) |
| 2018-01-01 12:10:00 | POINT (6.000 6.000) |
| 2018-01-01 12:15:00 | POINT (9.000 9.000) |
df = pd.DataFrame([
{'geometry':Point(3,3), 't':datetime(2018,1,1,12,0,0)},
{'geometry':Point(3,9), 't':datetime(2018,1,1,12,6,0)},
{'geometry':Point(2,9), 't':datetime(2018,1,1,12,10,0)},
{'geometry':Point(0,7), 't':datetime(2018,1,1,12,15,0)}
]).set_index('t')
geo_df = GeoDataFrame(df, crs=31256)
toy_traj2 = mpd.Trajectory(geo_df, 1)
toy_traj2.df
ax = toy_traj.plot()
toy_traj2.plot(ax=ax, color='red')
<AxesSubplot:>
print(f'Distance: {toy_traj.distance(toy_traj2)}')
print(f'Hausdorff distance: {toy_traj.hausdorff_distance(toy_traj2):.2f}')
Distance: 3.0 Hausdorff distance: 6.08
pt = Point(1, 5)
line = LineString([(3,3), (3,9)])
pt_gdf = GeoDataFrame(pd.DataFrame([{'geometry':pt, 'id':1}]))
line_gdf = GeoDataFrame(pd.DataFrame([{'geometry':line, 'id':1}]))
ax = toy_traj.plot()
pt_gdf.plot(ax=ax, color='red')
line_gdf.plot(ax=ax, color='red')
<AxesSubplot:>
print(f'Distance: {toy_traj.distance(pt)}')
print(f'Hausdorff distance: {toy_traj.hausdorff_distance(pt):.2f}')
Distance: 5.0 Hausdorff distance: 8.94
print(f'Distance: {toy_traj.distance(line)}')
print(f'Hausdorff distance: {toy_traj.hausdorff_distance(line)}')
Distance: 3.0 Hausdorff distance: 6.0